package com.xgame.server.netty.coder;

import com.xgame.server.netty.enity.MsgEntity;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.MessageToMessageEncoder;
import org.springframework.stereotype.Component;

import java.nio.CharBuffer;
import java.util.List;

//@Component
public class NettyMsgEncoder extends MessageToByteEncoder<MsgEntity> {
    @Override
    protected void encode(ChannelHandlerContext ctx, MsgEntity msg, ByteBuf byteBuf) throws Exception {
        byteBuf.writeInt(msg.getCmdCode());
        byteBuf.writeInt(msg.getTimeStamp());
        byteBuf.writeInt(msg.getReserved());
        byteBuf.writeInt(msg.getMsgLength());
        if (msg.getMsgLength() > 0) {
            byteBuf.writeBytes(msg.getData());
        }
    }
}
